package com.sjz.chat5;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

/***
 * 简单的component
 */
public class SimpleComponentActivity extends AppCompatActivity {
    private TextView textview_id;
    private EditText edittext_id;
    private Button button_id;
    private ImageButton imagebutton_id;

    private CheckBox football;
    private CheckBox pingpang;
    private CheckBox basketball;

    private RadioGroup rg_simple_sex;
    private RadioButton male;
    private RadioButton famale;
    private RadioButton nomale;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_component);

        textview_id=findViewById(R.id.textview_id);
        textview_id.setText("内容框");

        edittext_id=findViewById(R.id.edittext_id);

        button_id=findViewById(R.id.button_id);
        button_id.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String message= edittext_id.getText().toString();
                Toast.makeText(SimpleComponentActivity.this,message,Toast.LENGTH_SHORT).show();
            }
        });


        imagebutton_id=findViewById(R.id.imagebutton_id);
        imagebutton_id.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跟xml配置类似
                imagebutton_id.setBackgroundResource(android.R.drawable.alert_light_frame);
                imagebutton_id.setImageResource(android.R.drawable.ic_media_pause);
            }
        });


      //CheckBox 用法  isChecked()是否被选中
        football=findViewById(R.id.football);
        pingpang=findViewById(R.id.pingpang);
        basketball=findViewById(R.id.basketball);

        basketball.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(basketball.isChecked()){ //是否被选中
                      Toast.makeText(SimpleComponentActivity.this,basketball.getText().toString()+"选中",Toast.LENGTH_SHORT).show();
                } else{
                    Toast.makeText(SimpleComponentActivity.this,basketball.getText().toString()+"没选中",Toast.LENGTH_SHORT).show();
                }
            }
        });


        //单选组 单选
        rg_simple_sex=findViewById(R.id.rg_simple_sex);
        male=findViewById(R.id.male);
        famale=findViewById(R.id.famale);
        nomale=findViewById(R.id.nomale);
        //单选组点击事件触发
        rg_simple_sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {//checkedId 选中的单选按钮菜单
               RadioButton checkButton= findViewById(checkedId);
               String sex= checkButton.getText().toString();
               Toast.makeText(SimpleComponentActivity.this,sex,Toast.LENGTH_SHORT).show();
            }
        });


    }

    //点击按钮触发checkbox是否选中
    public void confirm(View v){
         StringBuilder sb=new StringBuilder();
         if(football.isChecked()){
             sb.append(football.getText().toString()+"   ");
         }
        if(pingpang.isChecked()){
            sb.append(pingpang.getText().toString()+"   ");
        }
        if(basketball.isChecked()){
            sb.append(basketball.getText().toString()+"   ");
        }

        Toast.makeText(SimpleComponentActivity.this,sb.toString()+"  选中了",Toast.LENGTH_SHORT).show();
    }


}